package com.smart.city.logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SensorLoggerActivity extends Activity {
	private MySensorLogger sensorLogger = null;
	private Button startButton = null;
	private Button endButton = null;
	private EditText FileNameText = null;
	private String Filename = "";


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(com.smart.city.R.layout.logger);

		this.sensorLogger = new MySensorLogger();
		this.sensorLogger.addSensor(new AccSensor(this));
		this.sensorLogger.addSensor(new LocationSensor(this));
	//	this.sensorLogger.addLogger(new LogcatLogger());

		this.FileNameText = (EditText) findViewById(com.smart.city.R.id.FileNameEdit);
		this.FileNameText.setText("");

		this.startButton = (Button) findViewById(com.smart.city.R.id.btn_start_logging);
		this.startButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				sensorLogger.loggers.clear();
				if(FileNameText.getText().length() <= 0){
					long now = System.currentTimeMillis();
					SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd,HH-mm-ss-SSS");
					String time = sdfNow.format(new Date(now));
					
					Filename = "/mnt/sdcard/tmp/" + time + ".txt";
				}
				else{
					
					Filename = "/mnt/sdcard/tmp/" + FileNameText.getText()+".txt";
				}
				sensorLogger.addLogger(new FileLogger(Filename));


				startButton.setEnabled(false);
				endButton.setEnabled(true);

				
				sensorLogger.startLogging();

			}
		});

		this.endButton = (Button) findViewById(com.smart.city.R.id.btn_end_logging);
		this.endButton.setEnabled(false);
		this.endButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startButton.setEnabled(true);
				endButton.setEnabled(false);
				sensorLogger.endLogging();

			}
		});

	}

	private class MySensorLogger extends SensorLogger {
		private Thread loggingThread = null; 
		private MySensorLogger instance;

		public MySensorLogger() {
			this.instance = this;
		}

		@Override
		public void endLogging() {

			this.loggingThread = null;
			int loggersSize = this.loggers.size();
			for (int loopi = 0; loopi < loggersSize; loopi++) {
				this.loggers.get(loopi).close();
				instance.loggers.get(loopi).close();
			}

			
		}

		@Override
		public void startLogging() {
			int loggersSize = this.loggers.size();
			for (int loopi = 0; loopi < loggersSize; loopi++) {
				instance.loggers.get(loopi).open();
			}
			this.loggingThread = new Thread() {	
				@Override
				public void run() {
					Thread me = Thread.currentThread();
					int loggersSize = instance.loggers.size();
					int sensorsSize = instance.sensors.size();

					while (instance.loggingThread == me) {
						long now = System.currentTimeMillis();
						SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss:SSS");
						String logMessage = sdfNow.format(new Date(now));

						for (int loopi = 0; loopi < sensorsSize; loopi++) {
							logMessage += instance.sensors.get(loopi).getFormatedString();
						}
						for (int loopi = 0; loopi < loggersSize; loopi++) {
							instance.loggers.get(loopi).write(logMessage);
						}
						try {
							//Thread.sleep(100);
							Thread.sleep(930);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			};
			this.loggingThread.start();
		}
	}
}
